package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.FileListMapper;
import com.lnking.lnkingplace.mapper.OutsidersMapper;
import com.lnking.lnkingplace.model.dto.exhibition.ExhibitionImport;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersDetailDto;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersImportDto;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersOverviewDto;
import com.lnking.lnkingplace.model.dto.user.UserDetailDto;
import com.lnking.lnkingplace.model.dto.user.UserOverviewDto;
import com.lnking.lnkingplace.model.entity.*;
import com.lnking.lnkingplace.model.request.outsiders.OutsidersEpidemicRequest;
import com.lnking.lnkingplace.service.IIsolationPlaceService;
import com.lnking.lnkingplace.service.IOutsidersService;
import com.lnking.lnkingplace.service.IPlaceInfoService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 外来人员登记表 服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-08-20
 */
@Service
public class OutsidersServiceImpl extends CustomServiceImpl<OutsidersMapper, Outsiders> implements IOutsidersService {

    @Value("${upload.file-path-prefix}")
    String filePathPrefix;

    @Resource
    FileListMapper fileListMapper;

    @Resource
    UserUtil userUtil;

    @Autowired
    IPlaceInfoService placeInfoService;

    public static final String  YEAR="年";
    /**
     * 去年，上个月
     */
    public static final long  LAST=1L;
    public static final Integer  NUM=0;


    @Override
    public OutsidersDetailDto getOutsidersDetailById(String id) {
        OutsidersDetailDto dto = getBaseMapper().getDetailById(id);
        return dto;
    }

    @Override
    public List<OutsidersOverviewDto> overview(OutsidersEpidemicRequest params) {
        List<OutsidersOverviewDto> overviewDtos=new ArrayList<>();
        if (params.getTimeType().equals(YEAR)) {
            params.setTime(LocalDateTime.now().minusYears(LAST));
        } else {
            params.setTime(LocalDateTime.now().minusMonths(LAST));
        }
        // 获取场所类型
        List<String> typeList = placeInfoService.getType();
        typeList.forEach(e->{
            params.setPlaceType(e);
            OutsidersOverviewDto dto = this.baseMapper.overview(params);
            if(dto ==null){
                dto=new OutsidersOverviewDto();
                dto.setType(e);
                dto.setNumber(NUM);
            }
            overviewDtos.add(dto);
        });

        return overviewDtos;
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<OutsidersImportDto> result =  ImportUtils.importExcel(file, OutsidersImportDto.class,option);
        for (OutsidersImportDto outsidersImportDto : result) {
            if (StringUtils.isNotBlank(outsidersImportDto.getName())) {
                Outsiders info = new Outsiders();
                BeanUtils.copyProperties(outsidersImportDto,info);
                info.setCreateTime(LocalDateTime.now());

                info.setChannel(userUtil.getUser().getChannel());
                if(StringUtils.isNotBlank(outsidersImportDto.getPlaceName())){
                    PlaceInfo check = placeInfoService.check(outsidersImportDto.getPlaceName());
                    Assert.notNull(check,"无该场所");
                    info.setPlaceId(check.getId());
                }

                if(StringUtils.isNotBlank(outsidersImportDto.getStartTime())){
                    info.setStartTime(HSSFDateUtil.getJavaDate(Double.parseDouble(outsidersImportDto.getStartTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                }

                if(StringUtils.isNotBlank(outsidersImportDto.getEndTime())){
                    info.setEndTime(HSSFDateUtil.getJavaDate(Double.parseDouble(outsidersImportDto.getEndTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                }
                this.baseMapper.insert(info);
            }

        }
    }
}
